// logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    phoneNumber: '',
    verifyCode: '',
    countdown: 0,
    timer: null
  },
  onLoad() {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return {
          date: util.formatTime(new Date(log)),
          timeStamp: log
        }
      })
    })
  },
  handlePhoneNumberInput(e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  handleVerifyCodeInput(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  getVerifyCode() {
    if (this.data.countdown > 0) return;

    // 模拟发送验证码
    wx.showToast({
      title: '验证码已发送',
      icon: 'none'
    });

    this.setData({
      countdown: 60
    });

    this.data.timer = setInterval(() => {
      if (this.data.countdown > 0) {
        this.setData({
          countdown: this.data.countdown - 1
        });
      } else {
        clearInterval(this.data.timer);
      }
    }, 1000);
  },
  login() {
    if (!this.data.phoneNumber || !this.data.verifyCode) {
      wx.showToast({
        title: '请输入手机号和验证码',
        icon: 'none'
      });
      return;
    }

    // 模拟验证码验证
    if (this.data.verifyCode === '123456') {
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
      // 跳转到首页或其他页面
      wx.switchTab({
        url: '/pages/index/index'
      });
    } else {
      wx.showToast({
        title: '验证码错误',
        icon: 'none'
      });
    }
  }
})